package com.example.testlxr9096.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.example.testlxr9096.R;
import com.example.testlxr9096.bean.Item;

import java.util.List;

public class SingleSelectAdapter extends RecyclerView.Adapter<SingleSelectAdapter.ViewHolder> {

    private List<Item> items;
    private int lastSelectedPosition = -1;

    private OnItemClickListener listener;

    public static int clickPosition = -1;

    public interface OnItemClickListener {
        void onItemClick(int position);
    }
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }



    public SingleSelectAdapter( List<Item> items) {
        this.items = items;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        Item item = items.get(position);

        // 设置文本
        holder.title.setText(item.getTitle());
        holder.title0.setText(item.getTitle0());

        // 设置背景
        Context context = holder.itemView.getContext();
        holder.title.setBackground(getItemBackground(context, item.getStatus(), item.isSelected()));

        // 点击事件
        holder.itemView.setOnClickListener(v -> {
            clickPosition = position;

            Log.d("readTaskposition","位置 +"+position);
            if (lastSelectedPosition != position) {
                // 取消上一次选中
                if (lastSelectedPosition != -1) {
                    items.get(lastSelectedPosition).setSelected(false);
                    notifyItemChanged(lastSelectedPosition);
                }

                // 设置当前选中
                item.setSelected(true);
                notifyItemChanged(position);
                lastSelectedPosition = position;
            }
            int pos = holder.getAdapterPosition();
            if (pos != RecyclerView.NO_POSITION) {
                if (listener != null) {
                    listener.onItemClick(position);
                }
            }
        });
    }
    private Drawable getItemBackground(Context context, int status, boolean isSelected) {


        int backgroundColor = getStatusColor(context, status);

        GradientDrawable background = new GradientDrawable();
        // 设置形状为圆形
        background.setShape(GradientDrawable.OVAL);
        background.setColor(backgroundColor);
        if (isSelected) {
            background.setStroke(2, Color.BLUE); // 2px宽，蓝色边框，可以根据需要调整
        }



        return background;
    }

    private int getStatusColor(Context context, int status) {
        switch (status) {
            case 1:
                return Color.GREEN;
            case 2:
                return ContextCompat.getColor(context, R.color.alerm);
            case 0:
                return ContextCompat.getColor(context, R.color.cycle_grey);

            default:
                return Color.WHITE;
        }
    }



    @Override
    public int getItemCount() {
        return items.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView title,title0;

        public ViewHolder(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.tv_title);
            title0 = itemView.findViewById(R.id.tv_title0);
        }
    }
}